<template>
  <el-dialog
    :visible.sync="visible"
    :title="dataForm.details ? '详情' : !dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="close()"
    width="70%"
    :destroy-on-close="true"
    @opened="opened()"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '140px' : '130px'"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="旅行社名称" prop="name">
            <el-input
              v-model="dataForm.name"
              :placeholder="disabled ? '' : '旅行社名称'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="旅行社编码" prop="code">
            <el-input
              v-model="dataForm.code"
              :placeholder="disabled ? '' : '旅行社编码'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人姓名" prop="linkName">
            <el-input
              v-model="dataForm.linkName"
              :placeholder="disabled ? '' : '联系人姓名'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人号码" prop="linkPhone">
            <el-input
              v-model="dataForm.linkPhone"
              :placeholder="disabled ? '' : '联系人号码'"
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="经度" prop="longitude">
            <el-input
              v-model="dataForm.longitude"
              :placeholder="disabled ? '' : '经度'"
              type="number"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="纬度" prop="latitude">
            <el-input
              v-model="dataForm.latitude"
              :placeholder="disabled ? '' : '纬度'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类型" prop="type">
            <SelectComponent
              ref="type"
              dictType="travel_type"
              v-model="dataForm.type"
            >
            </SelectComponent>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="评分" prop="score">
            <el-input
              v-model="dataForm.score"
              :placeholder="disabled ? '' : '评分'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="价格" prop="price">
            <el-input
              v-model="dataForm.price"
              :placeholder="disabled ? '' : '价格'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="dataForm.remark"
              :placeholder="disabled ? '' : '备注'"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="地址" prop="province">
            <el-select
              v-model="dataForm.province"
              :placeholder="disabled ? '' : '省编码'"
              clearable
              style="width: 14%"
              @change="
                (val) => {
                  regionChange(val, 'province');
                }
              "
            >
              <el-option
                v-for="item in provinceOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="dataForm.city"
              :placeholder="disabled ? '' : '市'"
              clearable
              style="width: 14%; margin: 0 1%"
              @change="
                (val) => {
                  regionChange(val, 'city');
                }
              "
            >
              <el-option
                v-for="item in cityOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="dataForm.area"
              :placeholder="disabled ? '' : '区'"
              clearable
              style="width: 14%"
              @change="
                (val) => {
                  regionChange(val, 'area');
                }
              "
            >
              <el-option
                v-for="item in areaOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
            <el-input
              style="width: 14%; margin: 0 1%"
              v-model="dataForm.street"
              :placeholder="disabled ? '' : '街道'"
            ></el-input>
            <el-input
              v-model="dataForm.areaDetail"
              :placeholder="disabled ? '' : '详细地址'"
              style="width: 40%"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="详情介绍" prop="info">
            <el-input
              v-model="dataForm.info"
              :placeholder="disabled ? '' : '详情介绍'"
              type="textarea"
              :rows="3"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import mixinRegionModule from "@/mixins/region-module";

export default {
  mixins: [mixinRegionModule],
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        name: "",
        code: "",
        linkName: "",
        linkPhone: "",
        province: "",
        city: "",
        area: "",
        street: "",
        areaDetail: "",
        longitude: "",
        latitude: "",
        type: "",
        score: "",
        info: "",
        remark: "",
        price: "",
      },
      tempLinkName: "",
      tempLinkPhone: "",
    };
  },
  props: {
    disabled: Boolean,
  },
  computed: {
    dataRule() {
      return {
        name: [
          {
            required: true,
            message: "请输入旅行社名称",
            trigger: "blur",
          },
        ],
        linkName: [
          {
            required: true,
            message: "请输入联系人姓名",
            trigger: "blur",
          },
        ],
        linkPhone: [
          {
            required: true,
            message: "请输入联系人号码",
            trigger: "blur",
          },
        ],
        latitude: [
          {
            required: true,
            message: "请输入纬度",
            trigger: "blur",
          },
          {
            validator: this.$formValidate.validateDecimal("纬度", 11, 8),
            trigger: ["blur"],
          },
        ],
        longitude: [
          {
            required: true,
            message: "请输入经度",
            trigger: "blur",
          },
          {
            validator: this.$formValidate.validateDecimal("经度", 11, 8),
            trigger: ["blur"],
          },
        ],
      };
    },
  },
  methods: {
    opened() {
      this.provincAreaDetailInfoList();
      if (this.dataForm.city) {
        this.cityAreaDetailInfoList(this.dataForm.province);
      }
      if (this.dataForm.area) {
        this.countyAreaDetailInfoList(this.dataForm.city);
      }
    },
    close() {
      this.dataForm = {
        id: "",
        name: "",
        code: "",
        linkName: "",
        linkPhone: "",
        province: "",
        city: "",
        area: "",
        street: "",
        areaDetail: "",
        longitude: "",
        latitude: "",
        type: "",
        score: "",
        info: "",
        remark: "",
        price: "",
      };
      this.$emit("changeDisabled");
    },

    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get("/sys/istravel/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          this.dataForm.type = this.dataForm.type + "";
          this.$refs.type.selectValue = this.dataForm.type;
          this.tempLinkName = this.dataForm.linkName;
          this.tempLinkPhone = this.dataForm.linkPhone;
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          //加密
          let newDataForm = Object.assign({}, this.dataForm);
          if (
            newDataForm.linkName &&
            this.tempLinkName != newDataForm.linkName
          ) {
            //编辑了
            newDataForm.linkName = this.$CryptoJS.encrypt(newDataForm.linkName);
          } else {
            newDataForm.linkName = null;
          }
          if (
            newDataForm.linkPhone &&
            this.tempLinkPhone != newDataForm.linkPhone
          ) {
            //编辑了
            newDataForm.linkPhone = this.$CryptoJS.encrypt(
              newDataForm.linkPhone
            );
          } else {
            newDataForm.linkPhone = null;
          }
          this.$http[!newDataForm.id ? "post" : "put"](
            "/sys/istravel/",
           newDataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>
